﻿using Microsoft.EntityFrameworkCore;
using Mojito.Data.Entities;
using Mojito.Model;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace Mojito.Services
{
    public class DirectoriesShareService : AppServiceBase
    {
        /// <summary>
        ///  根据工作区获取用户列表
        /// </summary>
        /// <param name="dirId"></param>
        /// <returns></returns>
        public async Task<IList<Sys_Account>> GetUserList(string dirId)
        {
            var list = Context.RequestShare
                .Where(d => d.DirectoryId == dirId)
                .Join(
                    Context.Sys_Account,
                    d => d.AccountId,
                    a => a.Id,
                    (d, a) => a)
                .ToListAsync();
            return await list;
        }

        /// <summary>
        ///  保存
        /// </summary>
        /// <param name="workspaceId"></param>
        /// <param name="userList"></param>
        /// <returns></returns>
        public async Task Save(ShareSaveInput input)
        {
            var oldList = Context.RequestShare
                .Where(d => d.DirectoryId == input.dirId);
            Context.RemoveRange(oldList);
            var newList = input.userList
                .Select(d => new RequestShare
                {
                    Id = Guid.NewGuid().ToString("N"),
                    AccountId = d,
                    DirectoryId = input.dirId,
                    Date = DateTime.Now
                });
            await Context.AddRangeAsync(newList);
            await Context.SaveChangesAsync();
        }
    }
}
